package uno.linze.interview_question.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.mail.MessagingException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import uno.linze.interview_question.domain.User;
import uno.linze.interview_question.dto.LoginRequest;
import uno.linze.interview_question.dto.PasswordResetDto;
import uno.linze.interview_question.dto.RegisterRequest;
import uno.linze.interview_question.dto.UserInfoDTO;
import uno.linze.interview_question.service.UserService;
import uno.linze.interview_question.mapper.UserMapper;
import org.springframework.stereotype.Service;
import uno.linze.interview_question.util.VerificationCodeService;
import uno.linze.interview_question.util.base.JwtUtil;

import java.util.Objects;

/**
* @author wangy
* @description 针对表【user(用户主表)】的数据库操作Service实现
* @createDate 2025-09-27 12:57:25
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private VerificationCodeService verificationCodeService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void sendRegisterVerifyCode(String email) {
        try {
            verificationCodeService.saveVerificationCode(email);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public UserInfoDTO register(RegisterRequest request) {
        if (userMapper.existsByUsername(request.getUsername())) {
            return null;
        }

        //验证验证码
        if (Objects.equals(verificationCodeService.getVerificationCode(request.getEmail()), request.getVerify_code())) {
            User user = new User();
            user.setUsername(request.getUsername());
            user.setEmail(request.getEmail());
            user.setNickname(request.getNickname());
            user.setTargetPosition(request.getTarget_position());
            user.setPassword(passwordEncoder.encode(request.getPassword()));
            userMapper.insert(user);

            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(user, userInfoDTO);
            return userInfoDTO;
        }

        return null;
    }

    @Override
    public String login(LoginRequest request) {
        User user= userMapper.selectByUsername(request.getUsername());
        if(user!=null&&passwordEncoder.matches(request.getPassword(),user.getPassword())){
            return JwtUtil.generateToken(user.getUsername());
        }
        return null;
    }

    @Override
    public UserInfoDTO getCurrentUserInfo(String token) {
        //从token中解析用户名
        String username = JwtUtil.getUsernameFromToken(token);
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            BeanUtils.copyProperties(user, userInfoDTO);
            return userInfoDTO;
        }
        return null;
    }

    @Override
    public UserInfoDTO updateUserInfo(UserInfoDTO userInfo,String token) {
        if(JwtUtil.getUsernameFromToken(token)!=null){
            User user = userMapper.selectByUsername(JwtUtil.getUsernameFromToken(token));
            if (user != null) {
                if(!StrUtil.isBlank(user.getNickname())){
                    user.setNickname(userInfo.getNickname());
                    userMapper.updateById(user);
                }
                BeanUtils.copyProperties(user, userInfo);
                return userInfo;
            }
        }
        return null;
    }

    @Override
    public void resetPassword(PasswordResetDto passwordResetDto, String token) {
        System.out.println("开始");
        if(passwordResetDto.getEmail().equals(userMapper.selectByUsername(JwtUtil.getUsernameFromToken(token)).getEmail())){
            //邮箱验证通过
            System.out.println("邮箱验证通过");
            User user = userMapper.selectByUsername(JwtUtil.getUsernameFromToken(token));
            if (user != null) {
                user.setPassword(passwordEncoder.encode(passwordResetDto.getNewPassword()));
                userMapper.updateById(user);
            }
        }
    }
}




